package edu.hnu.cloudflightserver.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtil {

	private static Properties pro = new Properties();
	static {
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
		System.out.println(Thread.currentThread().getContextClassLoader().getResource("").toExternalForm());
		try {
			pro.load(in);
			/*try {
				Class.forName(pro.getProperty("driver"));
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}*/
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("加载参数出现异常");
		}
	}

	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(pro.getProperty("url"), pro.getProperty("username"), pro.getProperty("password"));
	}

	public static void release(ResultSet rs, Connection con, Statement st) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (con != null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
